package com.example.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.common.Vo.*;
import com.example.common.annotation.LogAnno;
import com.example.common.constant.Const;
import com.example.common.result.Result;
import com.example.controller.base.BaseController;
import com.example.entity.KeyResults;
import com.example.entity.Objective;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotNull;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author ptuDai
 * @since 2022-04-19
 */
@RestController
@RequestMapping("/objective")
public class ObjectiveController extends BaseController {

    @PostMapping("/page")
    @PreAuthorize("hasAnyRole('user','admin','superAdmin')")
    public Result page(@RequestBody ObjectiveQuery objectiveQuery) {
        IPage<ObjectiveVO> pageObjectiveVO = new Page<>(objectiveQuery.getCurrent(), objectiveQuery.getSize());
        IPage<ObjectiveVO> page = this.objectiveService.selectPage(pageObjectiveVO,objectiveQuery);
        return Result.ok().data(page);
    }

    @LogAnno(operateType = "查看目标信息")
    @GetMapping("/info/{id}")
    @PreAuthorize("hasAnyRole('user','admin','superAdmin')")
    public Result info(@PathVariable @NotNull Integer id){
        return Result.ok().data(objectiveService.getById(id));
    }

    @LogAnno(operateType = "新增目标和关键结果")
    @PostMapping("/add/{type}")
    @PreAuthorize("hasAnyRole('user','admin','superAdmin')")
    @Transactional(rollbackFor = Exception.class)
    public Result add(@PathVariable @NotNull Integer type, @RequestBody EditObjectiveVO editObjectiveVO){
        objectiveService.add(type,editObjectiveVO);
        return Result.created();
    }

    @GetMapping("/parentObjective/{type}")
    @PreAuthorize("hasAnyRole('user','admin','superAdmin')")
    public Result parentObjective(@PathVariable @NotNull Integer type){
        List<Objective> parentObjectList = objectiveService.parentObjectList(type);
        return Result.ok().data(parentObjectList);
    }

    @GetMapping("/reviewer/{type}")
    @PreAuthorize("hasAnyRole('user','admin','superAdmin')")
    public Result reviewer(@PathVariable @NotNull Integer type){
        List<Reviewer> reviewerList = objectiveService.reviewerList(type);
        return Result.ok().data(reviewerList);
    }

    @LogAnno(operateType = "更新目标信息")
    @PostMapping("/update")
    @PreAuthorize("hasAnyRole('user','admin','superAdmin')")
    @Transactional(rollbackFor = Exception.class)
    public Result update(@RequestBody Objective objective){
        objective.setReview(Const.REVIEW_WAIT);
        objective.setStatus(Const.STATUS_WAIT);
        objectiveService.updateById(objective);
        return Result.update();
    }

    @LogAnno(operateType = "删除目标及关键结果")
    @DeleteMapping("/del/{id}")
    @PreAuthorize("hasAnyRole('user','admin','superAdmin')")
    @Transactional(rollbackFor = Exception.class)
    public Result del(@PathVariable @NotNull Integer id){
        objectiveService.removeById(id);
        keyResultsService.remove(new QueryWrapper<KeyResults>().eq("objective_id",id));
        return Result.deleted();
    }

    @LogAnno(operateType = "审核目标")
    @PostMapping("/review")
    @PreAuthorize("hasAnyRole('user','admin','superAdmin')")
    @Transactional(rollbackFor = Exception.class)
    public Result review(@RequestBody Objective objective){
        objectiveService.review(objective);
        return Result.ok();
    }

    @LogAnno(operateType = "评价目标")
    @PostMapping("/score")
    @PreAuthorize("hasAnyRole('user','admin','superAdmin')")
    public Result score(@RequestBody Objective objective){
        objectiveService.updateById(objective);
        return Result.ok();
    }

    @PreAuthorize("hasAnyRole('user','admin','superAdmin')")
    @GetMapping("/tree")
    public Result tree(){
        List<TreeObjectiveVO> tree = objectiveService.tree();
        return Result.ok().data(tree);
    }
}
